package com.tbit.main.util;

import java.text.SimpleDateFormat;

/**
 * 时间工具类
 *
 * @author Leon
 * 2019年1月23日 下午3:57:15
 */
public class TimeUtil {
    private static int day = 24 * 60 * 60;
    private static int hour = 60 * 60;
    private static int min = 60;

    public static String getDateTimeString(Integer time, String lang) {
        StringBuffer result = new StringBuffer();

        Integer days = 0;
        Integer hours = 0;
        Integer mins = 0;
        Integer secs = 0;

        if (time / day > 0) {
            days = time / day;
            hours = (time - day * days) / hour;
            mins = (time - day * days - hours * hour) / min;
            secs = time - day * days - hours * hour - min * mins;

            if (lang.contains("CN")) {
                result.append(days + "天").append(hours + "时").append(mins + "分").append(secs + "秒");
            } else {
                result.append(days + "D").append(hours + "H").append(mins + "M").append(secs + "S");
            }
        } else if (time / hour > 0) {
            hours = time / hour;
            mins = (time - hours * hour) / min;
            secs = time - hour * hours - min * mins;
            if (lang.contains("CN")) {
                result.append(hours + "时").append(mins + "分").append(secs + "秒");
            } else {
                result.append(hours + "H").append(mins + "M").append(secs + "S");
            }
        } else if (time / min > 0) {
            mins = time / min;
            secs = time - min * mins;

            if (lang.contains("CN")) {
                result.append(mins + "分").append(secs + "秒");
            } else {
                result.append(mins + "M").append(secs + "S");
            }
        } else {
            secs = time;

            if (lang.contains("CN")) {
                result.append(secs + "秒");
            } else {
                result.append(secs + "S");
            }
        }

        return result.toString();
    }

    /**
     * 两个时间差
     *
     * @param startTime
     * @param endTime
     * @param format
     * @return
     * @throws Exception
     */
    public static long dateDiff(String startTime, String endTime, String format) throws Exception {
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000 * 24 * 60 * 60;//一天的毫秒数
        long nh = 1000 * 60 * 60;//一小时的毫秒数
        long nm = 1000 * 60;//一分钟的毫秒数
        long ns = 1000;//一秒钟的毫秒数
        long diff;
        //计算两个时间的毫秒时间差异
        diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
        long day = diff / nd;
        long hour = diff % nd / nh;
        long min = diff / nm;
        long sec = diff % nd % nh % nm / ns;
        return min;
    }


    public static void main(String[] args) throws Exception {
        //System.out.println(getDateTimeString(360001, "TW"));
        System.out.println(dateDiff("2020-10-28 13:12:56", "2020-10-28 13:13:57", "yyyy-MM-dd HH:mm:ss"));
    }

}
